package com.tianhao.luo.service.impl;

import com.tianhao.luo.reply.impl.NormalPictureReplyAbleImpl;
import com.tianhao.luo.reply.impl.NormalTextReplyAbleImpl;
import com.tianhao.luo.reply.impl.NormalVoiceReplyAbleImpl;
import com.tianhao.luo.service.MessageService;
import com.tianhao.luo.utils.LuoSmartReply;
import com.tianhao.luo.utils.WeChatUtils;
import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author tianhao.luo
 * @date 2020/3/10 15:42
 */
@Service
public class MessageServiceImpl implements MessageService {
    private static final Logger LOGGER = LoggerFactory.getLogger(MessageServiceImpl.class);

    private static final String TO_USER_NAME = "ToUserName";
    private static final String FROM_USER_NAME = "FromUserName";
    private static final String CREATETIME = "CreateTime";
    private static final String MSGTYPE = "MsgType";
    private static final String CONTENT = "Content";

    private NormalTextReplyAbleImpl normalTextReplyAble = new NormalTextReplyAbleImpl();
    private NormalVoiceReplyAbleImpl normalVoiceReplyAble = new NormalVoiceReplyAbleImpl();
    private NormalPictureReplyAbleImpl normalPictureReplyAble = new NormalPictureReplyAbleImpl();
    private LuoSmartReply luoSmartReply = new LuoSmartReply(normalTextReplyAble,normalVoiceReplyAble,normalPictureReplyAble);

    @Autowired
    private WeChatUtils utils;

    @Override
    public String message(HttpServletRequest request){
        Map<String, String> map = null;
        try {
            map = utils.xmlToMap(request);
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put(TO_USER_NAME,map.get(FROM_USER_NAME));
            hashMap.put(FROM_USER_NAME,map.get(TO_USER_NAME));
            hashMap.put(CREATETIME,String.valueOf(System.currentTimeMillis()));
            String msgType = map.get(MSGTYPE);
            String message = "success";
            String content = "";
            String mediaId = "";
            switch (msgType) {
                case "text" :
                    content = map.get(CONTENT);
                    if (content.contains("笑话")) {
                        hashMap.put(MSGTYPE,"voice");
                        hashMap.put("MediaId",luoSmartReply.voiceReply(content));
                    }else if (content.contains("图片")) {
                        hashMap.put(MSGTYPE,"image");
                        hashMap.put("MediaId",luoSmartReply.pictureReply(content));
                    }else {
                        hashMap.put(MSGTYPE,msgType);
                        hashMap.put(CONTENT,luoSmartReply.textReply(content));
                    }
                    try {
                        LOGGER.info("{} success",content);
                        message = utils.mapToXml(hashMap);
                        break;
                    } catch (IOException | DocumentException e) {
                        e.printStackTrace();
                    }
                case "image":
                    hashMap.put(MSGTYPE,msgType);
                    mediaId = map.get("MediaId");
                    hashMap.put("MediaId",mediaId);
                    try {
                        message = utils.mapToXml(hashMap);
                        break;
                    } catch (IOException | DocumentException e) {
                        e.printStackTrace();
                    }
                case "voice":
                    hashMap.put(MSGTYPE,msgType);
                    mediaId = map.get("MediaId");
                    hashMap.put("MediaId",mediaId);
                    try {
                        message = utils.mapToXml(hashMap);
                        break;
                    } catch (IOException | DocumentException e) {
                        e.printStackTrace();
                    }
                default:
                    break;
            }
            return message;
        } catch (IOException | DocumentException e) {
            e.printStackTrace();
        }
        return "success";

    }

}
